package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.MateralDetailed;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.service.MateralDetailedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/materaldetailed")
public class MateralDetailedController {

    @Autowired(required = false)
    MateralDetailedService mds;

    //新增领料
    @ResponseBody
    @RequestMapping("/addmateraldetailed")
    public ResponseResult addMateralDetailed(MateralDetailed md){
        //数据验证
        ResponseResult rr;
        if(md.getNumber()<1){
            return rr = new ResponseResult(1,"数量不能小于1",0,null);
        }
        //插入数据
//        try {
            mds.insertMateralDetailed(md);
            rr = new ResponseResult(0,"领料成功",0,null);
//        }catch (Exception e){
//            rr = new ResponseResult(1,"领料失败",0,null);
//        }
        return rr;
    }

    //领料明细查询
    @ResponseBody
    @RequestMapping("/alldetailedmaterial")
    public ResponseResult selectAllDetailedMaterial(MateralDetailed materalDetailed,int page,int limit){
        List<MateralDetailed> lmd =  mds.selectAllDetailedMaterial(materalDetailed,page,limit);
        //查询总数
        int count = mds.selectDetailedMaterialSum(materalDetailed);
        ResponseResult rr= new ResponseResult(0,"",count,lmd);
        return rr;
    }

    //删除记录
    @ResponseBody
    @RequestMapping("/deletedetailedmaterial")
    public ResponseResult deleteDetailedDaterial(String madId){
        ResponseResult rr;
        try {
            mds.deleteDetailedDaterial(madId);
            rr = new ResponseResult(0,"删除成功",0,null);
        }catch(Exception e){
            rr = new ResponseResult(1,"删除失败",0,null);
        }
        return rr;
    }
}
